package com.wannengbang.cloudleader.homepage;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.AddressListBean;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.PayNumOrderBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.dialog.InputBuyNumDialog;
import com.wannengbang.cloudleader.event.ChooseAddressEvent;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.mine.AddressListActivity;
import com.wannengbang.cloudleader.mine.model.IMineModel;
import com.wannengbang.cloudleader.mine.model.MineModelImpl;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.ViewLoading;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExchangeMachinesActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_all_exchange)
    TextView tvAllExchange;
    @BindView(R.id.tv_convertible)
    TextView tvConvertible;
    @BindView(R.id.tv_redeemed)
    TextView tvRedeemed;
    @BindView(R.id.edit_num)
    EditText editNum;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.ll_select_address)
    LinearLayout llSelectAddress;
    @BindView(R.id.ll_exchange_bg)
    LinearLayout llExchangeBg;
    @BindView(R.id.tv_exchange_list)
    TextView tvExchangeList;
    @BindView(R.id.tv_buy)
    TextView tvBuy;
    @BindView(R.id.iv_check1)
    ImageView ivCheck1;
    @BindView(R.id.ll_deliver_mode1)
    LinearLayout llDeliverMode1;
    @BindView(R.id.iv_check2)
    ImageView ivCheck2;
    @BindView(R.id.ll_deliver_mode2)
    LinearLayout llDeliverMode2;
    private IMineModel mineModel;
    private AddressListBean.DataBean.ItemListBean addressBean;
    private int exchange_type = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange_machines);
        ButterKnife.bind(this);
        registerEventBus();
        titleBar.setRightClickListener(() -> {
            showActivity(ExchangeTypeActivity.class);
        });
        mineModel = new MineModelImpl();
        requestAddressIndex();
    }

    @Subscribe
    public void addressEvent(ChooseAddressEvent event) {
        addressBean = event.getItemListBean();
        tvName.setText(addressBean.getName());
        tvMobile.setText(addressBean.getMobile());
        tvAddress.setText(addressBean.getProvince() + addressBean.getCity() + addressBean.getArea() + addressBean.getAddress());
        tvAddress.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestUserInfo();
    }

    private void requestAddressIndex() {
        mineModel.requestAddressIndex(1, new DataCallBack<AddressListBean>() {
            @Override
            public void onSuccessful(AddressListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    addressBean = bean.getData().getItemList().get(0);
                    tvName.setText(addressBean.getName());
                    tvMobile.setText(addressBean.getMobile());
                    tvAddress.setText(addressBean.getProvince() + addressBean.getCity() + addressBean.getArea() + addressBean.getAddress());
                    tvAddress.setVisibility(View.VISIBLE);
                } else {
                    tvName.setText("暂无收货地址请添加");
                    tvAddress.setVisibility(View.GONE);
                }

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestUserInfo() {
        new MineModelImpl().requestMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {
                tvAllExchange.setText("赠送的机具总数量：" + bean.getData().getTotal_exchange() + "台");
                tvRedeemed.setText("" + bean.getData().getHave_exchange());
                tvConvertible.setText("" + bean.getData().getCan_exchange());
                //5标记隐藏
                if ("17329409714".equals(bean.getData().getMobile()) || "13403854628".equals(bean.getData().getMobile())) {
                    llExchangeBg.setBackgroundResource(R.mipmap.ic_withdraw_bg);
                } else {
                    llExchangeBg.setBackgroundResource(R.mipmap.ic_exchange_bj);
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }


    public void requestAgentExchangeDeviceAdd(String num) {
        ViewLoading.showProgress(mActivity, "提交中......");
        new HomePageModelImpl().requestAgentExchangeDeviceAdd(String.valueOf(addressBean.getId()), exchange_type + "", num, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort(bean.getMsg());
                showActivity(ExchangeMyListActivity.class);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestExchangeNumberBuyAdd(String num) {
        ViewLoading.showProgress(mActivity, "提交中......");
        new HomePageModelImpl().requestExchangeNumberBuyAdd(num, new DataCallBack<PayNumOrderBean>() {
            @Override
            public void onSuccessful(PayNumOrderBean bean) {
                String totalPrice = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getTotal_money()) / 100);
                Intent intent = new Intent(mActivity, PayNumberActivity.class);
                intent.putExtra("order_no", bean.getData().getId());
                intent.putExtra("totalPrice", totalPrice);
                startActivity(intent);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.ll_look1, R.id.ll_look2, R.id.tv_commit, R.id.ll_deliver_mode1, R.id.ll_deliver_mode2, R.id.ll_select_address, R.id.tv_buy, R.id.tv_exchange_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_look1:
                showActivity(ExchangeMyListActivity.class);
                break;
            case R.id.ll_look2:
                showActivity(ExchangeOtherListActivity.class);
                break;
            case R.id.ll_deliver_mode1:
                exchange_type = 1;
                ivCheck1.setImageResource(R.mipmap.ic_order_xuanze);
                ivCheck2.setImageResource(R.mipmap.ic_order_kongyuan);
                break;
            case R.id.ll_deliver_mode2:
                exchange_type = 2;
                ivCheck1.setImageResource(R.mipmap.ic_order_kongyuan);
                ivCheck2.setImageResource(R.mipmap.ic_order_xuanze);
                break;
            case R.id.tv_commit:
                if (addressBean == null) {
                    ToastUtil.showShort("请添加收货地址");
                    return;
                }
                String num = editNum.getText().toString();
                if (TextUtils.isEmpty(num)) {
                    ToastUtil.showShort("请输入兑换数量");
                    return;
                }
                requestAgentExchangeDeviceAdd(num);
                break;
            case R.id.ll_select_address:
                Intent intent = new Intent(mActivity, AddressListActivity.class);
                intent.putExtra("chooseType", "select_address");
                startActivity(intent);
                break;
            case R.id.tv_buy:
                InputBuyNumDialog dialog = InputBuyNumDialog.newInstance("1");
                dialog.show(getSupportFragmentManager(), "dialog");
                dialog.setOnCallBackListener(num1 -> {
                    requestExchangeNumberBuyAdd(String.valueOf(num1));
                });
                break;
            case R.id.tv_exchange_list:
                showActivity(ExchangeBuyNumListActivity.class);
                break;
        }
    }
}
